package work.shyk.cdp;


import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Map;

public class SendEmail {

	/**
	 * 日志
	 */
    private static final Logger logger = LoggerFactory.getLogger(SendEmail.class);

//
//	public void sendEmail(String subject, String content, String toAddress) {
//	    // 是否可以发送邮件开关
//	    boolean sendEmailEnabled = true;
//	    PropertiesUtil propertiesUtil = new PropertiesUtil();
//	    if (StringUtil.isNotBlank(propertiesUtil.getProperty("sendEmailEnabled"))) {
//	        sendEmailEnabled = Boolean.parseBoolean(
//	                propertiesUtil.getProperty("sendEmailEnabled"));
//        }
//
//	    if (!sendEmailEnabled || StringUtil.isBlank(toAddress)) {
//	        return;
//	    }
//
//    	String from = propertiesUtil.getProperty("from");
//    	String fromAddress = propertiesUtil.getProperty("fromAddress");
//    	String sendhost = propertiesUtil.getProperty("sendhost");
//    	String passwd = propertiesUtil.getProperty("passwd");
//
//    	MailSenderInfo mailSenderInfo = new MailSenderInfo();
//    	mailSenderInfo.setSubject(subject);
//    	mailSenderInfo.setContent(content);
//
//    	Properties props = System.getProperties();
//    	Session sendMailSession;
//
//    	props.put("mail.smtp.host", sendhost);
//    	props.put("mail.smtp.auth", "true");
//    	props.put("mail.transport.protocol", "smtp");
//    	MyAuthenticator myauth = new MyAuthenticator(from,passwd);
//    	sendMailSession = Session.getInstance(props, myauth);
//
//    	//Define message
//    	try{
//            MimeMessage message = new MimeMessage(sendMailSession);
//            message.setFrom(new InternetAddress(fromAddress));
//            if (toAddress.contains(",")) {
//                String[] toAddressArr = toAddress.split(",");
//                for (String toAddressTemp : toAddressArr) {
//                    if (StringUtil.isNotBlank(toAddressTemp)) {
//                        message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddressTemp.trim()));
//                    }
//                }
//            } else {
//                message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress.trim()));
//            }
//
//            message.setSubject(mailSenderInfo.getSubject());//加入邮件标题
//            message.setText(mailSenderInfo.getContent());//加入邮件内容
//            Transport.send(message);// 发送邮件
//        } catch (Exception e) {
//            logger.error(e.getMessage(), e);
//        }
//    }


    public static Map<String, Object> sendEmail(MailSenderInfo mailSenderInfo) {
        ExchangeClient exchangeClient = new ExchangeClient.ExchangeClientBuilder()
                .hostname(mailSenderInfo.getMailServerHost())
                .exchangeVersion(ExchangeVersion.Exchange2010)
                .username(mailSenderInfo.getUserName())
                .password(mailSenderInfo.getPassword())
                .subject(mailSenderInfo.getSubject())
                .message(mailSenderInfo.getContent())
                .recipientTo(mailSenderInfo.getToAddress())
                .attachments(Arrays.asList(mailSenderInfo.getAttachFileNames()))
                .build();
        return exchangeClient.sendExchange();
    }

    public static void main(String args[]){
        StringBuilder sb = new StringBuilder();
        sb.append("<br/><br/>操作帮助，请查看<a href='https://confluence.paas.sinopec.com/pages/viewpage.action?pageId=11338319'>帮助中心</a>" +
                "<br/>如有问题，请联系<a href='http://confluence.paas.sinopec.com/pages/viewpage.action?pageId=11339414'>技术支持</a>" +
                "<br/><br/>注：此邮件为持续测试系统（CTS）自动发送的邮件，请勿直接回复。");
        MailSenderInfo mailSenderInfo = new MailSenderInfo();
        mailSenderInfo.setMailServerHost("mail.sinopec.com");
        mailSenderInfo.setUserName("zhihui.zhou");
        mailSenderInfo.setPassword("SHyk102591243");
        mailSenderInfo.setToAddress("zhihui.zhou@pcitc.com");
        mailSenderInfo.setSubject("发邮件测试");
        mailSenderInfo.setContent(sb.toString());
//        mailSenderInfo.setAttachFileNames(new String[]{"D:\\IDEA\\apache-tomcat-9.0.14.tar.gz", "D:\\IDEA\\exam_system.sql", "D:\\IDEA\\config.properties"});
        mailSenderInfo.setAttachFileNames(new String[]{"asdf"});
        System.out.println(sendEmail(mailSenderInfo));
    }
}
